package com.ht.action;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import com.ht.model.History;
import com.ht.service.HistoryService;
import com.ht.util.Constants;
import com.ht.util.MyPage;

public class HistoryAction extends ActionSupport {

	History history ;
	HistoryService historyService ;

	public History getHistory() {
		return history;
	}

	public void setHistory(History history) {
		this.history = history;
	}

	public HistoryService getHistoryService() {
		return historyService;
	}

	public void setHistoryService(HistoryService historyService) {
		this.historyService = historyService;
	}

	public String doAdd() throws  Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = historyService.addHistory(history);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to add......");
			target = "failure";
		} else if (flag == 1) {
			request.setAttribute("info", "Add items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.HIS_ACTION);
			target="success";
		} else {
			request.setAttribute("info", "Already exist......");
			target = "failure";
		}
		return target;
	}
	
	public String delete() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		int flag = historyService.deleteHistory(history);
		PrintWriter out = response.getWriter();
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to delete......");
			target = "failure";
		} else {
			request.setAttribute("info", "Delete items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.HIS_ACTION);
			target="success";
		}
		return target;
	}

	
	public String toUpdate()throws Exception  {
		History sa = historyService.getHistoryById(history);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("history", sa);
		return "edit";
	}
	

	public String doUpdate() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		PrintWriter out = response.getWriter();
		int flag = historyService.updateHistory(history);
		
		String target =null; 
		if (flag == 0) {
			request.setAttribute("info", "Failed to update......");
			target = "failure";
		} else {
			request.setAttribute("info", "Update items Success......");
			request.setAttribute("path", getBasePath(request) + Constants.HIS_ACTION);
			target="success";
		}
		return target;
	}

	public String select() throws Exception {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String historyId = request.getParameter("id");
		String name = request.getParameter("name");
		History history = new History();
		int hisId = 0;
		try { 
			hisId = Integer.parseInt(historyId);
			} catch (Exception e) { 
				hisId = 999999;
			} 

		if (historyId != null && !"".equals(historyId)) {
			history.setId(hisId);
		}
		String tempPage = request.getParameter("curPage");
		int curPage = 1;
		if(tempPage != null){
			curPage = new Integer(tempPage);
		}
		MyPage mypage = historyService.selectByCon(history, curPage);
		request.setAttribute("mypage", mypage);
		return "index";
		

	}
	
	public String check()throws Exception  {
		History sa = historyService.getHistoryById(history);
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("history", sa);
		return "check";
	}


	public String getBasePath(HttpServletRequest request) {
		String basePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + request.getContextPath();
		return basePath;
	}
	

}
